package com.ruix.app.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.model.Comment;

/**
 * Created by hadexs on 2017/5/21.
 */

public class CommentPostDialog extends DialogFragment{

    private Comment replayComment;
    private EditText leaveWords;
    private TextView sendMessage;
    private CommentListener listener;


    public static CommentPostDialog newInstance(Comment comment,CommentListener commentListener){
        CommentPostDialog dialog = new CommentPostDialog();
        dialog.replayComment = comment;
        dialog.listener = commentListener;
        return dialog;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo_Light_DialogWhenLarge_NoActionBar);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View contentView =inflater.inflate(
                R.layout.layout_circle_comment, null);
        leaveWords = (EditText) contentView.findViewById(R.id.et_leave_words);
        sendMessage = (TextView) contentView.findViewById(R.id.tv_send_message);
        leaveWords.requestFocus();
        //
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        Window dialogWindow = getDialog().getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);

        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//        WindowManager wm = (WindowManager) getContext()
//                .getSystemService(Context.WINDOW_SERVICE);
//        int width = wm.getDefaultDisplay().getWidth();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height =  WindowManager.LayoutParams.WRAP_CONTENT;//lp.WRAP_CONTENT;
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialogWindow.setAttributes(lp);
        initListener();
        iniView();
        return contentView;
    }

    private void iniView() {
        if (replayComment != null) {
            String name = replayComment.getAuthorName();
            if (!TextUtils.isEmpty(name)) {
                leaveWords.setHint(String.format("回复：%s", name));
            }
        }
    }

    private void initListener(){
        leaveWords.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0){
                    sendMessage.setEnabled(true);
                } else {
                    sendMessage.setEnabled(false);
                }
            }
        });
        sendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.submitCommentAction(sendMessage.getText().toString().trim());
                }
                dismiss();
            }
        });
    }

    public interface CommentListener{
        void  submitCommentAction(String commentStr);
    }
}
